import { bus } from "/utils/core";
import validator from "/utils/validators";
import { showToast } from "/utils/common";
import { freeSourceType, unitType } from "/utils/dictionary";
import request from "/utils/omsRequest";

Component({
  mixins: [],
  data: {
    title: "",
    services: [],
    freeSourceType,
    tabIndex: 0,
    scrollLeft: 0,
    tabList: ["审核中", "审核通过", "审核拒绝"],
    checkRecord: [],
    form: {
      contractGroupId: null,
      contractFreeServiceList: []
    },
    unitType
  },
  props: {
    show: false,
    onClose: () => { }
  },
  didMount() {
    bus.subscribe("onFreeServiceCheckInfo", (contract) => {
      this.setData({
        title: contract.merchantName,
        form: {
          contractGroupId: contract.id,
          contractFreeServiceList: []
        }
      });
      dd.showLoading({
        content: '加载中...',
      });
      request({
        url: "/ajax/contractFreeService/list",
        data: {
          contractGroupId: contract.id
        }
      }).then(res => {
        dd.hideLoading();
        this.data.checkRecord[0] = res.voList.filter(item => item.processResult === "REVIEW");
        this.data.checkRecord[1] = res.voList.filter(item => item.processResult === "AGREE");
        this.data.checkRecord[2] = res.voList.filter(item => item.processResult === "REFUSE");
        this.data.checkRecord.forEach((element, index) => {
          if (element.length > 0) {
            this.setData({
              tabIndex: index
            });
            return;
          }
        });
        this.setData({
          checkRecord: this.data.checkRecord
        });
      })
    });
    request({
      url: "/ajax/freeService/list"
    }).then(res => {
      this.setData({
        services: res.voList
      });
    });

  },
  didUpdate() { },
  didUnmount() {
    bus.remove("onFreeServiceCheckInfo")
  },
  methods: {
    hideModal(e) {
      this.props.onClose(e);
    },
    onTabSelect(e) {
      this.setData({
        tabIndex: e.currentTarget.dataset.index,
        scrollLeft: (e.currentTarget.dataset.index - 1) * 60
      });
    },
    onServiceChange(obj) {
      if (obj.index === null && obj.form) {
        this.data.form.contractFreeServiceList.push(obj.form);
      } else if (obj.index !== null && obj.form) {
        this.data.form.contractFreeServiceList.splice(obj.index, 1, obj.form);
      } else if (obj.index !== null && !obj.form) {
        this.data.form.contractFreeServiceList.splice(obj.index, 1);
      }
      this.setData({
        "form.contractFreeServiceList": this.data.form.contractFreeServiceList
      });
    },
    onSubmit() {
      let validate = new validator();
      validate.add("checkEmptyArray", this.data.form.contractFreeServiceList, "请至少添加一个增值服务");
      if (validate.check()) {
        dd.showLoading({
          content: '加载中...',
        });
        request({
          url: "/ajax/contractFreeService/createContractFreeService",
          method: "post",
          data: this.data.form,
          headers: {
            "Content-Type": "application/json;utf-8"
          }
        }).then(res => {
          dd.hideLoading();
          showToast({
            type: "success",
            msg: "提交成功!"
          });
          dd.reLaunch({
            url: '/pages/contract/contract/contract'
          });
        })
      }
    }
  },
});
